package zabbix

import (
	"encoding/json"
	"errors"
)

func ScriptGetscriotsbyhostsData() (string,map[string]interface{}) {
	method := "script.getscriptsbyhosts"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "30079",
        "30073"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ScriptCreateData() (string,map[string]interface{}) {
	method := "script.Create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Reboot server",
        "command": "reboot server 1",
        "host_access": 3,
        "confirmation": "Are you sure you would like to reboot the server?"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ScriptDeleteData() (string,map[string]interface{}) {
	method := "script.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "3",
        "4"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ScriptUpdateData() (string,map[string]interface{}) {
	method := "script.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "scriptid": "1",
        "command": "/bin/ping -c 10 {HOST.CONN} 2>&1"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ScriptExecuteData() (string,map[string]interface{}) {
	method := "script.execute"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "scriptid": "1",
        "hostid": "30079"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ScriptGetData() (string,map[string]interface{}) {
	method := "script.gets"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ScriptGetscriotsbyhosts(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ScriptGetscriotsbyhostsData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ScriptCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ScriptCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ScriptDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ScriptDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ScriptUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ScriptUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ScriptExecute(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ScriptExecuteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ScriptGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ScriptGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



